<?php

Yii::import('application.models._base.BaseAuctionInfo');
Yii::import("application.models.dao.auction.AuctionDao");
Yii::import('dao.auction.AuctionDetailDao');

class AuctionInfo extends BaseAuctionInfo
{
	public $keywords;
	public $limit = 10;
	public $search_end_time;
	public $search_category_id;
	public $category_id_arr;
	public $search_status = 2;
	
	public $title;
	public $images;
	public $orderBy='t.end_time ASC';
	
	public $winner_name;
	public $winner_avatar;
	public $winner_price;
	public $value_price;
	public $offset;
	
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}

	public function behaviors(){
	    return array(
	        'CTimestampBehavior' => array(
	            'class' => 'zii.behaviors.CTimestampBehavior',
	            'createAttribute' => 'created',
	            'updateAttribute' => 'modified',
	        ),
	        'CAuctionPoolBehavior' => array(
	            'class'    => 'application.models.behaviors.CAuctionPoolBehavior',
	            'auctionIdAttribute'   =>  'id',
	            'startTimeAttribute'   =>  'start_time',
	            'endTimeAttribute'     =>  'end_time',
	            'winnerPriceIncrementAttribute' => 'winner_price_increment',
	            'winnerPrice' => 'winner_price',    
	        ),
	        'CAuctionCacheBehavior' => array(
	            'class'    => 'application.models.behaviors.CAuctionCacheBehavior',
	            'auctionIdAttribute'       =>  'auction_id',
	            'auctionStatusAttribute'   =>  'status',
	        )
	    );
	}
	
	static public function getDetailUrl($title,$id){
	    return Yii::app()->baseUrl."/auction-".AuctionInfo::urlencode($title)."-".$id;
	}
	
	static  public function urlencode($url){
	    $url = str_replace(array('(',')','.','$','-',':','/','//','\\'),null,$url);
        $url=str_replace(array(' ','&','+','?','#','%','='), "_", $url);
	    if(strlen($url) > 15){
	        $url = substr($url,0,15);
	    }
	    return urlencode($url);
	}
	
	public function rules(){
	    return array_merge(parent::rules(),array(
	       array('start_date,end_date','safe'),
	       array('keywords','safe','on'=>'search'),
	    ));
	}
	public function relations()
	{
		return array(
		    'Orders'=>array(self::HAS_ONE, 'Orders', 'auction_id'),
		    'AuctionPool'=>array(self::HAS_ONE, 'AuctionPool', 'auction_id'),
		);
	}
	public function setStart_date($date){
	    return $this->start_time = strtotime($date);
	}
	
	public function getStart_date(){
	    $date = null;
	    if ($this->start_time > 0){
	        $date = date('Y/m/d H:i:s', $this->start_time);
	    }
	    return $date;
	}
	
	public function setEnd_date($date){
	    return $this->end_time = strtotime($date);
	}
	
	public function getEnd_date(){
	    $date = null;
	    if ($this->end_time > 0){
	       $date = date('Y/m/d H:i:s', $this->end_time);
	    }
	    return $date;
	}
	public function getCustomer($id=null){
	    if(!empty($id)){
	        $customer=Customer::model()->findByPk($id);
	        return $customer;
	    }
	    
	}
	public function attributeLabels() {
		return array(
				'id' => Yii::t('app', 'ID'),
				'title' => Yii::t('app', 'Title'),
				'tag' => Yii::t('app', 'Tag'),
				'short_description' => Yii::t('app', 'Short Description'),
				'description' => Yii::t('app', 'Description'),
				'created' => Yii::t('app', 'Created'),
				'modified' => Yii::t('app', 'Modified'),
				'status' => Yii::t('app', 'Status'),
				'is_delete' => Yii::t('app', 'Is Delete'),
		);
	}
	
	public function search() {
		$criteria = new CDbCriteria;
		if(!empty($this->keywords)){
			$criteria->compare('title', $this->keywords, true,'OR');
		}
		$criteria->compare('title', $this->title,true);
		$criteria->compare('status',$this->status);
		$criteria->addCondition('is_delete=0');
		
		return new CActiveDataProvider($this, array(
				'criteria' => $criteria,
		));
	}
	
	/**
	 * 前台结束auction查找
	 */
	public function search_closed() {
	    $criteria = new CDbCriteria;
	    $criteria->join=' LEFT JOIN auction_parame AS t2 on t.id=t2.auction_id ';
	    $criteria->compare('status',2);
	    $criteria->order="t.end_time DESC";
	    $criteria->addCondition('is_delete=0');	
	    $criteria->addCondition("t2.parame_name='show' AND t2.parame_value=1");
	    
	    if (!empty($this->offset)) {
	        $criteria->offset=$this->offset;
	    }
	    
	    return new CActiveDataProvider($this, array(
	        'criteria' => $criteria,
	        'pagination'=>array(
	            'pageSize'=>$this->limit,
	        ),
	    ));
	}
	public function search_my(){
		$criteria = new CDbCriteria;
		$criteria->addCondition('customer_id='.yii::app()->user->id);
		$criteria->addCondition('type=1 and t.status=2');
		$criteria->join='JOIN orders ON orders.auction_id=t.id';
		$criteria->addCondition('t.is_delete=0');
		return  $a=new CActiveDataProvider($this, array(
			'criteria' => $criteria,
			'pagination'=>array(
				'pageSize'=>5, //当前页数显示记录条数
				),

		));
	}
	
	static public function getTitle($auctionId){
	    $auctionInfo=new AuctionInfo();
        $auctionInfo=$auctionInfo->findByPk($auctionId);
        $title="";
	    if (!empty($auctionInfo)){
	        $title= $auctionInfo->title;
	    }
	    return $title;
	}
	
	public static function getInitPrice($auctionId){
	    $auctionDao=new AuctionDao();
	    $auctionDao->find($auctionId);
	    return $auctionDao->autobid_startprice;
	}
	
	public static function getImageUrl($auctionId){
	    $auctionDao=new AuctionDao();
	    $auctionDao->find($auctionId);
	    $result=Yii::app()->resource->path('img') . '/no-image.png';
	    if (!empty($auctionDao->images["image_1"])){
	        $result=$auctionDao->images["image_1"];
	    }
	    return $result;
	}
}